`
https://leetcode.cn/problems/sliding-window-maximum/
`

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var maxSlidingWindow = function (nums, k) {
  const deque = [];
  const res = [];

  for (let i = 0; i < nums.length; i++) {
    // 移除队尾小于当前元素的元素
    while (deque.length && nums[i] >= nums[deque[deque.length - 1]]) {
      deque.pop();
    }
    deque.push(i);

    // 移除不在窗口内的队首元素
    if (deque[0] <= i - k) {
      deque.shift();
    }

    // 当窗口形成时记录结果
    if (i >= k - 1) {
      res.push(nums[deque[0]]);
    }
  }

  return res;
};

// test
const nums = [1, 3, -1, -3, 5, 3, 6, 7];
const k = 3;
console.log(maxSlidingWindow(nums, k)); // [3, 3, 5, 5, 6, 7]